package com.y.architecture;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

/**
 * Created by youjingjing on 2017/11/14.
 */

public class PaintCanvas extends View{

    public PaintCanvas(Context context) {
        super(context,null);
    }

    public PaintCanvas(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs,0);
    }

    public PaintCanvas(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }


    /*save和restore要配对使用（restore可以比save少，但不能多）*/
    @Override
    protected void onDraw(Canvas canvas) {

        Paint paint = new Paint();

        paint.setColor(Color.parseColor("#cccccc"));

        canvas.drawRect(0, 0, getMeasuredWidth(), getMeasuredHeight(), paint);
        Paint paint1 = new Paint();
        paint1.setColor(Color.parseColor("#000000"));
        canvas.drawRect(10, 10, getMeasuredWidth() - 10, getMeasuredHeight() - 10, paint1);

        /*save 用来保存Canvas的状态。save之后，可以调用Canvas的平移、放缩、旋转、错切、裁剪等操作*/
        canvas.save();
        canvas.translate(100, 10);
        super.onDraw(canvas);
        paint.setColor(Color.parseColor("#cccccc"));
        /*restore 用来恢复Canvas之前保存的状态。防止save后对Canvas执行的操作对后续的绘制有影响*/
        canvas.restore();
        canvas.drawRect(30, 30, getMeasuredWidth() - 30, getMeasuredHeight() - 30, paint);


//        int px = getMeasuredWidth();
//        int py = getMeasuredWidth();
//
//// Draw background
//        canvas.drawRect(0, 0, px, py, paint);
//
//        canvas.save();
//        canvas.rotate(90, px / 2, py / 2);
//
//// Draw up arrow
//        canvas.drawLine(px / 2, 0, 0, py / 2, paint1);
//        canvas.drawLine(px / 2, 0, px, py / 2, paint1);
//        canvas.drawLine(px / 2, 0, px / 2, py, paint1);
//
//        canvas.restore();
//
//// Draw circle
//        canvas.drawCircle(px - 10, py - 10, 10, paint1);

    }

}
